//
//  QLPublishLifeClassifyVC.m
//  神约
//
//  Created by qlwl on 17/2/10.
//  Copyright © 2017年 qlwl. All rights reserved.
//

#import "QLPublishLifeClassifyVC.h"
#import "QLPublishClassifyModel.h"
#import "QLPublishSelectPayTypeVC.h"
#import "QLPublishSeekHelpVC.h"

#define InteritemSpacing (8)
#define InterNum (5)
#define CellWidth (iphoneWidth-(InterNum - 1)*InteritemSpacing - 2 * 8)/InterNum
#define CellHeight (CellWidth)

@interface QLPublishLifeClassifyVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@end

@implementation QLPublishLifeClassifyVC
{
    NSArray * _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout).minimumLineSpacing = InteritemSpacing;
    ((UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout).minimumInteritemSpacing = InteritemSpacing;
    
    [self netWorking];
}

#pragma mark CollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _dataArray.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    QLPublishClassifyModel * firstModel = _dataArray[section];
    return firstModel.childlist.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    UICollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QLPublishClassifyCollCell" forIndexPath:indexPath];
    QLPublishClassifyModel * firstModel = _dataArray[indexPath.section];
    QLPublishClassifyModel * secondModel = firstModel.childlist[indexPath.row];
    UIButton * btn = [cell viewWithTag:10];
    [btn setTitle:secondModel.name forState:UIControlStateNormal];
    return cell;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(CellWidth, CellHeight);
    
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0,8,0,8);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    QLPublishClassifyModel * firstModel = _dataArray[indexPath.section];
    QLPublishClassifyModel * secondModel = firstModel.childlist[indexPath.row];
    if (self.category == 1) {
        QLPublishSelectPayTypeVC * payTypeVC = [NSString instantiateViewControllerWithIdentifier:@"QLPublishSelectPayTypeVC" andSBName:@"Home"];
        payTypeVC.classifyId = secondModel.id;
        payTypeVC.name = secondModel.name;
        [self.navigationController pushViewController:payTypeVC animated:YES];
    }else {
        QLPublishSeekHelpVC * helpVC = [NSString instantiateViewControllerWithIdentifier:@"QLPublishSeekHelpVC" andSBName:@"Home"];
        helpVC.category = self.category;
        helpVC.classifyId = secondModel.id;
        helpVC.name = secondModel.name;
        [self.navigationController pushViewController:helpVC animated:YES];
    }
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView * reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"QLPublishClassifyCollReusableView" forIndexPath:indexPath];
    QLPublishClassifyModel * firstModel = _dataArray[indexPath.section];
    UILabel * label = [reusableView viewWithTag:10];
    label.text = firstModel.name;
    return reusableView;
}

- (BOOL) shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender
{
    return YES;
}

#pragma mark netWorking
- (void) netWorking
{
    NSMutableDictionary * postDict = [[NSMutableDictionary alloc] init];
    postDict[@"type"] = [NSString stringWithFormat:@"%d",self.category];
    [QLNetWorking postJSONWithUrl:QLPublishLifeClissifyURL parameters:postDict isJosnRequest:YES rogress:nil success:^(id result) {
        NSLog(@"%@",result);
        if ([QLNetWorking sucess:[result objectForKey:@"mobileHead"] and:@"code" EqualToString:@"success"]) {
            _dataArray = [QLPublishClassifyModel mj_objectArrayWithKeyValuesArray:[[result objectForKey:@"mobileBody"] objectForKey:@"data"]];
            [self.collectionView reloadData];
        }
    } fail:^(NSError *error) {
        
    }];
}


@end
